/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor.mdi.Common;

import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import javax.swing.undo.UndoManager;

import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;

/**
 *
 * @author Eray
 */
public class Tools
{
    public static UndoManager undoManager = new UndoManager();
    
    // <editor-fold defaultstate="collapsed" desc="Static Declerations">

    private static String XmlFilesPath = "";
    private static String ScrapFilesPath = "";

    private static String StartupPath = "";
    private static String MikTeXPath = "";
    private static String LastOpenedDirectory = "";
    private static String LastSavedDirectory = "";
    private static String LastExportedDirectory = "";

    private static String XmlFileName_Toolbars = "Toolbars.xml";
    private static String XmlFileName_ToolbarButtons = "ToolbarButtons.xml";
    private static String XmlFileName_TextEditors = "TextEditors.xml";
    private static String ComponentName_ToolbarInitial = "toolbar_";
    private static String ComponentName_ToolbarButtonInitial = "toolbarButton_";

    public static boolean SaveInScrapFiles = false;

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Properties for Static Variables">

    /**
     * @return the XmlFilesPath
     */
    public static String getXmlFilesPath() {
        return XmlFilesPath;
    }

    /**
     * @param aXmlFilesPath the XmlFilesPath to set
     */
    public static void setXmlFilesPath(String aXmlFilesPath) {
        XmlFilesPath = aXmlFilesPath;
    }

    /**
     * @return the ScrapFilesPath
     */
    public static String getScrapFilesPath() {
        return ScrapFilesPath;
    }

    /**
     * @param aScrapFilesPath the ScrapFilesPath to set
     */
    public static void setScrapFilesPath(String aScrapFilesPath) {
        ScrapFilesPath = aScrapFilesPath;
    }

    /**
     * @param aLastSavedDirectory the LastSavedDirectory to set
     */
    public static void setLastSavedDirectory(String aLastSavedDirectory) {
        LastSavedDirectory = aLastSavedDirectory;
    }

    /**
     * @return the StartupPath
     */
    public static String getStartupPath() {
        return StartupPath;
    }

    /**
     * @param aStartupPath the StartupPath to set
     */
    public static void setStartupPath(String aStartupPath) {
        StartupPath = aStartupPath;
    }

    /**
     * @return the LastOpenedDirectory
     */
    public static String getLastOpenedDirectory() {
        return LastOpenedDirectory;
    }

    /**
     * @param aLastOpenedDirectory the LastOpenedDirectory to set
     */
    public static void setLastOpenedDirectory(String aLastOpenedDirectory) {
        LastOpenedDirectory = aLastOpenedDirectory;
    }

    /**
     * @return the LastSavedDirectory
     */
    public static String getLastSavedDirectory() {
        return LastSavedDirectory;
    }

    /**
     * @return the LastExportedDirectory
     */
    public static String getLastExportedDirectory() {
        return LastExportedDirectory;
    }

    /**
     * @param aLastExportedDirectory the LastExportedDirectory to set
     */
    public static void setLastExportedDirectory(String aLastExportedDirectory) {
        LastExportedDirectory = aLastExportedDirectory;
    }

    /**
     * @return the SaveInScrapFiles
     */
    public static boolean isSaveInScrapFiles() {
        return SaveInScrapFiles;
    }

    /**
     * @param aSaveInScrapFiles the SaveInScrapFiles to set
     */
    public static void setSaveInScrapFiles(boolean aSaveInScrapFiles) {
        SaveInScrapFiles = aSaveInScrapFiles;
    }

    /**
     * @return the MikTeXPath
     */
    public static String getMikTeXPath() {
        return MikTeXPath;
    }

    /**
     * @param aMikTeXPath the MikTeXPath to set
     */
    public static void setMikTeXPath(String aMikTeXPath) {
        MikTeXPath = aMikTeXPath;
    }

    /**
     * @return the XmlFileName_Toolbars
     */
    public static String getXmlFileName_Toolbars() {
        return XmlFileName_Toolbars;
    }

    /**
     * @param aXmlFileName_Toolbars the XmlFileName_Toolbars to set
     */
    public static void setXmlFileName_Toolbars(String aXmlFileName_Toolbars) {
        XmlFileName_Toolbars = aXmlFileName_Toolbars;
    }

    /**
     * @return the XmlFileName_ToolbarButtons
     */
    public static String getXmlFileName_ToolbarButtons() {
        return XmlFileName_ToolbarButtons;
    }

    /**
     * @param aXmlFileName_ToolbarButtons the XmlFileName_ToolbarButtons to set
     */
    public static void setXmlFileName_ToolbarButtons(String aXmlFileName_ToolbarButtons) {
        XmlFileName_ToolbarButtons = aXmlFileName_ToolbarButtons;
    }

    /**
     * @return the XmlFileName_TextEditors
     */
    public static String getXmlFileName_TextEditors() {
        return XmlFileName_TextEditors;
    }

    /**
     * @param aXmlFileName_TextEditors the XmlFileName_TextEditors to set
     */
    public static void setXmlFileName_TextEditors(String aXmlFileName_TextEditors) {
        XmlFileName_TextEditors = aXmlFileName_TextEditors;
    }

    /**
     * @return the ComponentName_ToolbarInitial
     */
    public static String getComponentName_ToolbarInitial() {
        return ComponentName_ToolbarInitial;
    }

    /**
     * @param aComponentName_ToolbarInitial the ComponentName_ToolbarInitial to set
     */
    public static void setComponentName_ToolbarInitial(String aComponentName_ToolbarInitial) {
        ComponentName_ToolbarInitial = aComponentName_ToolbarInitial;
    }

    /**
     * @return the ComponentName_ToolbarButtonInitial
     */
    public static String getComponentName_ToolbarButtonInitial() {
        return ComponentName_ToolbarButtonInitial;
    }

    /**
     * @param aComponentName_ToolbarButtonInitial the ComponentName_ToolbarButtonInitial to set
     */
    public static void setComponentName_ToolbarButtonInitial(String aComponentName_ToolbarButtonInitial) {
        ComponentName_ToolbarButtonInitial = aComponentName_ToolbarButtonInitial;
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">

    public Tools()
    {

    }
    /**
     * Specify the <b>Relative</b> paths for <i>XmlFilesPath</i> and <i>ScrapFilesPath</i>.
     * The <i>StartupPath</i> and <i>MikTeXPath</i> should be specified as
     * the physical paths (Ex: C:\\ProjectFolder\\).
     *
     * @param XmlFilesPath
     * @param StartupPath
     */
    public Tools(String XmlFilesPath, String StartupPath)
    {
        Tools.XmlFilesPath = XmlFilesPath;
        Tools.StartupPath = StartupPath;

        Tools.LastOpenedDirectory = StartupPath;
        Tools.LastSavedDirectory = StartupPath;
        Tools.LastExportedDirectory = StartupPath;
    }
    /**
     * Specify the <b>Relative</b> paths for <i>XmlFilesPath</i> and <i>ScrapFilesPath</i>.
     * The <i>StartupPath</i> and <i>MikTeXPath</i> should be specified as
     * the physical paths (Ex: C:\\ProjectFolder\\).
     *
     * @param XmlFilesPath
     * @param ScrapFilesPath
     * @param StartupPath
     * @param MikTeXPath
     */
    public Tools(String XmlFilesPath, String ScrapFilesPath, String StartupPath, String MikTeXPath)
    {
        Tools.XmlFilesPath = XmlFilesPath;
        Tools.ScrapFilesPath = ScrapFilesPath;
        Tools.StartupPath = StartupPath;
        Tools.MikTeXPath = MikTeXPath;
        
        Tools.LastOpenedDirectory = StartupPath;
        Tools.LastSavedDirectory = StartupPath;
        Tools.LastExportedDirectory = StartupPath;
    }

    // </editor-fold>

    
    /**
     * Loads the MDI editors from their specific XML files. After the load, they are dynamically created and displayed on the form.
     *
     * @param TabEditors
     */
    public void InitializeTextEditors(JTabbedPane TabEditors)
    {
        //MDI Starts
        TextEditors ClsTextEditor = new TextEditors(getXmlFileName_TextEditors());
        ClsTextEditor.LoadEditors(TabEditors);
    }
    public void ButtonPressed(JButton Button, boolean IsPressed, String ButtonText)
    {
        Button.setText(ButtonText);
        Button.setEnabled(IsPressed);
    }
   
    /**
     * Searches the component name inside the given JFrame component. Since
     * getting the components of a JFrame requires a ContentPane, it is
     * strongly advised to use a JPanel inside the JFrame.
     *
     * <p>
     * If the component cannot be found, it returns <b>null</b>.
     * </p>
     * 
     * @param Form
     * @param componentName
     * @return
     */
    public Component GetComponent(JFrame Form, String componentName)
    {
        return GetComponent(Form.getContentPane(), componentName);
    }

    public Component GetComponent(Component component, String componentName) {
        Component found = null;

        //if(!child.getName().equals(""))
            //JOptionPane.showMessageDialog(null, component.getName());
        
        if (component.getName() != null && component.getName().equals(componentName)) {
            found = component;
        } else {
                for (Component child : ((Container) component).getComponents()) {
                        

            found = GetComponent(child, componentName);
                        if (found != null)
                                break;
                }
            }
        return found;
    }

    /**
     * Sorts the Integer values given inside an ArrayList.
     * The sorted ArrayList will be returned.
     *
     * @param x
     * @return
     */
    public ArrayList<Integer> SortArray(ArrayList<Integer> x)
    {
        int n = x.size();
        for (int pass=1; pass < n; pass++)
        {  // count how many times
            // This next loop becomes shorter and shorter
            for (int i=0; i < n-pass; i++)
            {
                if (x.get(i) > x.get(i+1))
                {
                    // exchange elements
                    int temp = x.get(i);
                    x.set(i, x.get(i+1));
                    x.set(i+1, temp);
                }
            }
        }
        return x;
    }
    /**
     * It generates a unique Integer value in a given ArrayList.
     * This functions gets the <b>first</b> missing Integer value
     * of the ArrayList and returns it.
     *
     * <p>
     * Give the ArrayList to be searched and generated a unique Integer
     * value. Also, specify the minimum Integer value you want to be
     * generated by giving the Starting Index.
     * </p>
     *
     *
     * @param Array
     * @param StartIndex
     * @return
     */
    public int GenerateId(ArrayList<Integer> Array, int StartIndex)
    {
        int ID = StartIndex;
        int i, datacount = 0;

        ArrayList<Integer> arrayID = new ArrayList<Integer>();

        for(Integer integer : Array)
        {
            arrayID.add(integer);
            datacount++;
        }

        arrayID = SortArray(arrayID);

        for (i = 0; i < datacount; i++)
        {
            if (ID == arrayID.get(i).intValue())
                ID++;
        }

        return ID;
    }
}
